/*
 * Copyright (C) 2011-2015 ShenZhen iBOXPAY Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary
 * information of iBoxPay Company of China. 
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with iBoxpay inc.
 *
 */

package com.iboxpay.sdk;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * The class MapUtil.
 *
 * Description: 
 *
 * @author: nieminjie
 * @since: 2016年2月29日	
 * @version: $Revision$ $Date$ $LastChangedBy$
 *
 */

public class MapUtil {

	/**获取 Map<String, String>String值
	 * @param key
	 * @param map
	 * @return
	 */
	public static String getStringValue(String key, Map<String, String> map) {
		if (null != map) {
			HashMap<String, String> inMap = (HashMap<String, String>) map;
			for (Entry<String, String> en : inMap.entrySet()) {
				if (en.getKey().equals(key)) {
					return String.valueOf(en.getValue());
				}

			}
		}
		return "";
	}

	/**
	 * 获取 Map<String, Object>Object值
	 * @param key
	 * @param map
	 * @return
	 */
	public static String getObjectValue(String key, Map<String, Object> map) {
		if (null != map) {
			HashMap<String, Object> inMap = (HashMap<String, Object>) map;
			for (Entry<String, Object> en : inMap.entrySet()) {
				if (en.getKey().equals(key)) {
					return String.valueOf(en.getValue());
				}

			}
		}

		return "";
	}

	/**
	 * Map<String, Object> 转为 Map<String, String>
	 * @param map
	 * @return
	 */
	public static Map<String, String> tranObj2Str(Map<String, Object> map) {
		if (null != map) {
			HashMap<String, Object> inMap = (HashMap<String, Object>) map;
			Map<String, String> outMap = new HashMap<String, String>();
			for (Entry<String, Object> en : inMap.entrySet()) {
				outMap.put(en.getKey(), String.valueOf(en.getValue()));
			}
			return outMap;
		}
		return null;
	}

	/**
	 * Map<String, String> 转为 Map<String, Object>
	 * @param map
	 * @return
	 */
	public static Map<String, Object> tranStr2Obj(Map<String, String> map) {
		if (null != map) {
			HashMap<String, String> inMap = (HashMap<String, String>) map;
			Map<String, Object> outMap = new HashMap<String, Object>();
			for (Entry<String, String> en : inMap.entrySet()) {
				outMap.put(en.getKey(), en.getValue());
			}
			return outMap;
		}
		return null;
	}

	/**
	 * 排序拼接成xxx=xxx
	 * @param signMap
	 * @return
	 */
	public static String sortAppend(Map<String, ?> signMap) {

		// 需签名的字符串
		StringBuilder toSign = new StringBuilder();
		if (null != signMap && !signMap.isEmpty()) {
			Set<String> names = signMap.keySet();
			List<String> list = new ArrayList<String>(names.size());
			list.addAll(names);

			// 排序
			Collections.sort(list);

			for (int i = 0; i < list.size(); i++) {
				String name = list.get(i);
				Object value = signMap.get(name);
				if (null == value || "".equals(value) || "null".equals(value)) {
					continue;
				}

				// 基本数据类型
				if (ObjectUtil.isBasicDataType(value.getClass()) || value instanceof String) {
					toSign.append(name + "=" + value);
				} else {
					toSign.append(getObjectString(value));
				}

				if (i < list.size() - 1) {
					toSign.append("&");
				}
			}

			// 去除最后的&
			if (toSign.length() > 0) {
				if (String.valueOf(toSign.charAt(toSign.length() - 1)).equals("&")) {
					toSign.deleteCharAt(toSign.length() - 1);
				}
			}
		}
		return toSign.toString();
	}

	/**
	 * 获取对象的字符串，格式为key=value&...
	 * @param value
	 * @return
	 */
	private static String getObjectString(Object value) {
		if (null == value || "".equals(value) || "null".equals(value)) {
			return "";
		}

		// 基本数据类型
		if (ObjectUtil.isBasicDataType(value.getClass()) || value instanceof String) {
			return String.valueOf(value);
		} else {
			return getObjectString(value, new StringBuilder()).toString();
		}

	}

	/**
	 * 获取对象的字符串，格式为key=value&...
	 * @param value
	 * @return
	 */
	private static StringBuilder getObjectString(Object value, StringBuilder toSign) {

		// 值为基本数据类型
		if (ObjectUtil.isBasicDataType(value.getClass()) || value instanceof String) {
			if (toSign.length() > 0) {
				toSign.append("&");
			}
			toSign.append(String.valueOf(value));
		} else if (value instanceof Collection) {

			// 值为集合类型
			Collection tmpColl = (Collection) value;
			if (tmpColl.isEmpty()) {
				return toSign;
			}

			Iterator it = tmpColl.iterator();
			while (it.hasNext()) {
				Object next = it.next();
				if (null == value || "".equals(value) || "null".equals(value)) {
					continue;
				}

				if (toSign.length() > 0) {
					toSign.append("&");
				}

				// 基本数据类型
				if (ObjectUtil.isBasicDataType(next.getClass()) || next instanceof String) {
					toSign.append(String.valueOf(next));
				} else {
					toSign.append(getObjectString(next));
				}
			}
		} else {
			Map tmpMap = null;
			if (value instanceof Map) {
				tmpMap = (Map) value;
			} else {
				tmpMap = BeanUtil.toMap(value);
			}
			if (tmpMap.isEmpty()) {
				return toSign;
			}

			// key列表
			List<String> keys = new ArrayList<String>(tmpMap.keySet());

			// 排序
			Collections.sort(keys);

			for (String key : keys) {
				Object tmpValue = tmpMap.get(key);
				if (null == tmpValue || "".equals(tmpValue) || "null".equals(tmpValue)) {
					continue;
				}

				if (toSign.length() > 0) {
					toSign.append("&");
				}

				// 基本数据类型
				if (ObjectUtil.isBasicDataType(tmpValue.getClass()) || tmpValue instanceof String) {
					toSign.append(key).append("=").append(String.valueOf(tmpValue));
				} else {
					toSign.append(getObjectString(tmpValue));
				}
			}
		}
		return toSign;
	}
}
